package com.myapp.ruyixing.ui;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class DistanceMenuFragment extends ListFragment implements
		OnItemClickListener {

	private String[] items = new String[] { "500m", "1km", "2km", "5km", "全部" };

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		setListAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, items));
		getListView().setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (getActivity() instanceof NearbyStoreActivity) {
			((NearbyStoreActivity) getActivity()).changeDistance(""
					+ getVaule(position), items[position]);
		} else if (getActivity() instanceof FoodListActivity) {
			((FoodListActivity) getActivity()).changeDistance(""
					+ getVaule(position), items[position]);
		}
	}

	private int getVaule(int position) {
		switch (position) {
		case 0:
			return 500;

		case 1:
			return 1000;

		case 2:
			return 2000;

		case 3:
			return 5000;

		case 4:
			return 0;

		default:
			return 0;
		}
	}
}
